// Copyright (c) 2023 刻BITTER
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.


// #include <data_basic.hpp>

#include "app.hpp"
// #include "between_face_common.hpp"
// #include "modbus_client.hpp"
#include "oled.hpp"
// #include "ui.hpp"
#include "screen.hpp"
#include "storage.hpp"


/////////////////////// PIN MAPPING /////////////////////////

const ioxx::Pin LED_PIN = m128::LED_PIN;


/////////////////////// TOP LEVEL GLOBALS /////////////////////////

Scheduler dcb;


///////////////////////  MAIN    ////////////////////////////

TimeType task_blink() {
    static int8_t flag = -1;
    LED_PIN.toggle();

    if (flag > 3) {
        flag = -1;
    }
    show_battery(flag);
    ++flag;

    return 800;
}


void yield() {
    dcb.tick();
}


void loop() {
    enter_and_switch_screen();  // 进入当前界面并处理导航
}


void setup() {
    // Wire.begin();

    Serial.begin(115200);

    pinMode(LED_PIN, OUTPUT);

    // 先延时一会儿，等屏幕上电
    delay(200);
    init_oled();
    init_buttons();

    dcb.add_task(task_blink, 0);
    dcb.add_task(task_scan_button, 0);
    dcb.add_task(task_update_storage, 0);

    setup_menu_item();
    init_modbus_client_by_menu();

    // 显示个欢迎界面
    show_logo();
    delay(1000);
}